package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.TeachingMajorService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.service.MajorItemBankService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-07-08 11:44:50 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/majorItemBank")
public class MajorItemBankController extends BaseController {
	@Autowired
	private MajorItemBankService majorItemBankService;
    @Autowired
    private UserService userService;
    @Autowired
    private TeachingMajorService teachingMajorService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:majorItemBank:majorItemBank")
	String MajorItemBank(){
	    return "jzweb/majorItemBank/majorItemBank";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:majorItemBank:majorItemBank")
	public Result<Page<MajorItemBankDO>> list(MajorItemBankDO majorItemBankDTO){
        Wrapper<MajorItemBankDO> wrapper = new EntityWrapper<MajorItemBankDO>().orderBy("id", false);

        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            if(roleName.contains("讲师") && !roleName.contains("培训机构")){
                wrapper.eq("creater_id",this.getUserId());
            }
        }

        Page<MajorItemBankDO> page = majorItemBankService.selectPage(getPage(MajorItemBankDO.class), wrapper);

		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			MajorItemBankDO obj = (MajorItemBankDO) page.getRecords().get(i);
            UserDO userDO = userService.selectById(obj.getCreaterId());
            if(userDO!=null && userDO.getUsername()!=null){
                obj.setCreaterName(userDO.getUsername());
            }else {
                obj.setCreaterName("");
            }
            //教学专业名称
            TeachingMajorDO teachingMajorDO = teachingMajorService.selectById(obj.getTeacherMajorId());
            if(teachingMajorDO!=null && teachingMajorDO.getName()!=null){
                obj.setTeacherMajorName(teachingMajorDO.getName());
            }else {
                obj.setTeacherMajorName("");
            }
			list.add(obj);
		}
		if(list.size()>0) {
            page.setRecords(list);
        }else {
            page.setRecords(null);
        }
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:majorItemBank:add")
	String add(Model model){
        Wrapper<TeachingMajorDO> wrapper = new EntityWrapper<TeachingMajorDO>().orderBy("id", false);

        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            if(roleName.contains("讲师") && !roleName.contains("培训机构")){
                wrapper.eq("creater_id",this.getUserId());
            }
        }
        List<TeachingMajorDO> teachingMajorDOS = teachingMajorService.selectList(wrapper);
        model.addAttribute("teachingMajor",teachingMajorDOS);
	    return "jzweb/majorItemBank/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:majorItemBank:edit")
	String edit(@PathVariable("id") Long id,Model model){
        Wrapper<TeachingMajorDO> wrapper = new EntityWrapper<TeachingMajorDO>().orderBy("id", false);

        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            if(roleName.contains("讲师") && !roleName.contains("培训机构")){
                wrapper.eq("creater_id",this.getUserId());
            }
        }
        List<TeachingMajorDO> teachingMajorDOS = teachingMajorService.selectList(wrapper);
        model.addAttribute("teachingMajor",teachingMajorDOS);

		MajorItemBankDO majorItemBank = majorItemBankService.selectById(id);
		model.addAttribute("majorItemBank", majorItemBank);
	    return "jzweb/majorItemBank/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:majorItemBank:add")
	public Result<String> save( MajorItemBankDO majorItemBank){
        majorItemBank.setCreaterDate(new Date());
        majorItemBank.setCreaterId(this.getUserId());
		majorItemBankService.insert(majorItemBank);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:majorItemBank:edit")
	public Result<String>  update( MajorItemBankDO majorItemBank){
	    System.out.println("url:"+majorItemBank.getResourceUrl());
        majorItemBank.setUpdaterId(this.getUserId());
		boolean update = majorItemBankService.updateById(majorItemBank);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:majorItemBank:remove")
	public Result<String>  remove( Long id){
		majorItemBankService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:majorItemBank:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		majorItemBankService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
